package com.anyun.collect.adapter

import android.app.Activity
import android.app.ProgressDialog
import android.os.Handler
import android.os.Looper
import android.text.SpannableStringBuilder
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.anyun.collect.R
import com.anyun.collect.bean.PointData
import com.anyun.collect.util.FileUtil

/**collect
 * Created by lzw on 2019/12/5. 16:18:07
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
class PointListAdapter(context:Activity,strs:List<PointData>):RecyclerView.Adapter<PointListAdapter.ViewHolder>(){

    val mContext = context
    var  datas = strs

    lateinit var progressDialog:ProgressDialog
    companion object{
        val TAG:String = PointListAdapter::class.java.simpleName
    }

    class ViewHolder(
        itemView:View
    ) : RecyclerView.ViewHolder(itemView) {
        var tvWhichPoint:TextView = itemView.findViewById(R.id.tv_which_pint)
        var et_Xpoint :EditText = itemView.findViewById(R.id.et_x_point)
        var et_Ypoint :EditText = itemView.findViewById(R.id.et_y_point)

        var btn_collect:Button = itemView.findViewById(R.id.btn_collect)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.list_item_point,null,false);
        return ViewHolder(inflate)
    }

    override fun getItemCount(): Int {
            return datas.size;
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        Log.i(TAG,"position"+position)
        holder.btn_collect.setOnClickListener {
            showProgressDialog("",String.format("正在采集%d号点，请稍候...",position+1))

        startCaiji()
        }
        holder.tvWhichPoint.text = String.format("%d.",datas[position].whichPoint )
        holder.et_Xpoint.text = SpannableStringBuilder(String.format("%.3f", datas[position].xPoint))
        holder.et_Ypoint.text=SpannableStringBuilder(String.format("%.3f",datas[position].yPoint))

    }

    private fun startCaiji() {
        var handler:Handler = Handler()
        FileUtil.writeTxtFileToSD("map.json","[{\n" +
                "  \"id\": 863,\n" +
                "  \"item\": 1,\n" +
                "  \"point\": [{\n" +
                "    \"x-y\": [-11.9669, 28.013, -11.5114, 27.137, -10.8069, 27.5039, -10.2969, 26.4212, -9.2102, 26.8894, -9.6513, 28.0027, -8.9758, 28.3797, -9.5057, 29.3232]\n" +
                "  }\n" +
                "  ]\n" +
                "},\n" +
                "  {\n" +
                "    \"id\": 3000,\n" +
                "    \"item\": 2,\n" +
                "    \"point\": [{\n" +
                "      \"x-y\": [-11.9669, 28.013, -11.5114, 27.137, -10.8069, 27.5039, -10.2969, 26.4212, -9.2102, 26.8894, -9.6513, 28.0027, -8.9758, 28.3797, -9.5057, 29.3232]\n" +
                "    }\n" +
                "    ]\n" +
                "  }\n" +
                "]")
        handler.postDelayed({ hideProgressDialog()

        },4000)
    }


    /*
     * 提示加载
     */
    fun showProgressDialog(title: String?, message: String?) {
        if (!this::progressDialog.isInitialized) {
            progressDialog = ProgressDialog.show(
                mContext, title,
                message, true, false
            )
        } else if (progressDialog.isShowing) {
            progressDialog.setTitle(title)
            progressDialog.setMessage(message)
        }
        progressDialog.setTitle(title)
        progressDialog.setMessage(message)
        progressDialog.show()
    }

    /*
     * 隐藏提示加载
     */
    fun hideProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing) {
            progressDialog.dismiss()
        }
    }
}